/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyClass;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author HuuTai_it
 */
public class HighScoresSave {
    
    private RecordStore rStore;
    
    public HighScoresSave () {
        
        openRS();
               
        try {
            if(rStore.getNumRecords() == 0)  {

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);

                for(int i = 0; i < 5; i++)
                    dos.writeInt(0);

                rStore.addRecord(baos.toByteArray(), 0, baos.toByteArray().length);
            }

        }
        catch(RecordStoreNotFoundException rsnfe){}
        catch(RecordStoreException rse){}
        catch(Exception ioe){}
        
        closeRS();
    }
    
    public void openRS(){
        
        try {rStore = RecordStore.openRecordStore("HighScores", true);}
        catch(RecordStoreException ex){}
    }

    public void closeRS(){
        
        try{rStore.closeRecordStore();}
        catch (RecordStoreNotOpenException ex){}
        catch (RecordStoreException ex){}
    }
    
    public int updateHighScores(int hs) throws Exception {
        
        int[] temp = new int[5];
        int rank = 5;
        temp = getHighScores();
        
        for(int i = 0; i < 5; i++)
            if(hs == temp[i]) {
                rank = 5;
                i = 5;
            }
            else
                if(hs > temp[i]) {
                    rank = i;
                    i = 5;
            }
        if(rank < 5) {
            
            for(int i = 4; i > rank; i--)
                temp[i] = temp[i - 1];
            
            temp[rank] = hs;
            
            openRS();
            
            try {
            
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);

                for(int i = 0; i < 5; i++)
                    dos.writeInt(temp[i]);

                rStore.setRecord(1, baos.toByteArray(), 0, baos.toByteArray().length);
            }
            catch(RecordStoreNotFoundException rsnfe){}
            catch(RecordStoreException rse){}
            catch(IOException ioe){}
            
            closeRS();
        }  
        return rank;
    }
    
    public int[] getHighScores() throws Exception {
        
        int[] temp = new int[5];
        byte[] b = null;

        openRS();
        try{

                b = rStore.getRecord(1);
                ByteArrayInputStream bin =  new ByteArrayInputStream( b );

                DataInputStream din = new DataInputStream( bin );
                
                for(int i = 0; i < 5; i++)
                    temp[i] = din.readInt();
                
                bin.reset();
                din.close();
                din = null;
        }
        catch (IOException e){}
        catch(RecordStoreException recordstoreexception){}
        
        closeRS();
        return temp;
    }    
}